package cn.edu.nju.ws.aquila.rdf.canonicalization.comparator;

import java.util.ArrayList;
import java.util.Comparator;

import cn.edu.nju.ws.aquila.rdf.canonicalization.labeled.LabeledResource;

/**
 * @author Gong Cheng
 * 
 */
public class RDFGraphComparator implements
		Comparator<ArrayList<ArrayList<LabeledResource>>> {
	private Comparator<ArrayList<LabeledResource>> statementComparator = null;

	private RDFGraphComparator() {
		/* Nothing */
	}

	public RDFGraphComparator(
			Comparator<ArrayList<LabeledResource>> statementComparator) {
		this.statementComparator = statementComparator;
	}

	public int compare(ArrayList<ArrayList<LabeledResource>> g1,
			ArrayList<ArrayList<LabeledResource>> g2) {
		int result = g1.size() - g2.size();
		if (result != 0) {
			return result;
		}

		for (int i = 0; i < g1.size(); i++) {
			ArrayList<LabeledResource> s1 = g1.get(i);
			ArrayList<LabeledResource> s2 = g2.get(i);

			result = this.statementComparator.compare(s1, s2);
			if (result != 0) {
				return result;
			}
		}

		return 0;
	}

	public void close() {
		this.statementComparator = null;
	}
}